package com.example.mybatisplusdemo.service;

import com.example.mybatisplusdemo.common.AdvancedFlightQueryWrapper;
import com.example.mybatisplusdemo.mapper.FlightMapper;
import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.domain.ItineraryRequest;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdvancedFlightSearchService {

    private final FlightMapper flightMapper;
    private final FlightSearchService basicSearchService;

    public AdvancedFlightSearchService(
            FlightMapper flightMapper,
            FlightSearchService basicSearchService) {
        this.flightMapper = flightMapper;
        this.basicSearchService = basicSearchService;
    }

    public List<Flight> searchWithAdvancedFilters(ItineraryRequest request) {
        // 1. 先执行基础搜索
        List<Flight> flights = basicSearchService.searchFlights(request);

        // 2. 应用无法通过SQL实现的复杂过滤
        return flights.stream()
                .filter(f -> meetsAdvancedCriteria(f, request))
                .sorted(getAdvancedComparator(request))
                .collect(Collectors.toList());
    }

    private boolean meetsAdvancedCriteria(Flight flight, ItineraryRequest request) {
        // 实现那些无法通过SQL直接过滤的条件
        // 例如复杂的优惠活动逻辑
        return true;
    }

    private Comparator<Flight> getAdvancedComparator(ItineraryRequest request) {
        // 实现那些无法通过SQL直接排序的复杂逻辑
        return Comparator.comparing(Flight::getPrice);
    }
}